﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SubjectGroupRepository.cs" company="ELEN 7045 GROUP II">
//   Contributed by E.V.
// </copyright>
// <summary>
//   The SubjectGroup Repository Implementation
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Infrastructure.Data.StudyDefinition.Repositories
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Domain.StudyDefinition.Contracts.Repositories.SubjectGroups;
    using Domain.StudyDefinition.Entities;

    using Infrastructure.Common.Data;
    using System.Linq.Expressions;

    /// <summary>
    /// The SubjectGroup Repository Implementation
    /// </summary>
    public class SubjectGroupRepository : Repository<SubjectGroup>, ISubjectGroupRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SubjectGroupRepository"/> class.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        public SubjectGroupRepository(IContext context)
            : base(context)
        {
        }

        /// <summary>
        /// Adds the specified item.
        /// </summary>
        /// <param name="item">The item to add.</param>
        /// <returns>A item which was added</returns>
        public override SubjectGroup Add(SubjectGroup item)
        {
            return this.Context.SubjectGroups.Add(item);
        }

        /// <summary>
        /// Updates the specified item.
        /// </summary>
        /// <param name="item">The item to update.</param>
        public override void Update(SubjectGroup item)
        {
            this.Context.Entry(item).State = System.Data.EntityState.Modified;
        }

        /// <summary>
        /// Deletes the specified item.
        /// </summary>
        /// <param name="item">The item to delete.</param>
        public override void Delete(SubjectGroup item)
        {
            var subjectGroup = this.Context.SubjectGroups.Find(item.Id);

            this.Context.SubjectGroups.Remove(subjectGroup);
        }

        /// <summary>
        /// Gets a set of items with the specified predicates.
        /// </summary>
        /// <param name="predicates">The predicates to get the set of items by.</param>
        /// <returns>An Enumerable set of subject groups</returns>
        public override IEnumerable<SubjectGroup> Get(params Expression<Func<SubjectGroup, bool>>[] predicates)
        {

            var query = from s in this.Context.SubjectGroups.Include("Subjects")
                        select s;

            if (predicates != null)
            {
                foreach (var condition in predicates)
                {
                    query = query.Where(condition);
                }
            }

            return query.ToArray();
        }
    }
}
